package light.head.framework.base;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.config.ConfigParm;
import light.head.constant.Parm;
import light.head.constant.Sys;
import light.head.dao.sys.OperationLogDAO;
import light.head.framework.editor.CustomDatetimeEditor;
import light.head.framework.entity.PageBean;
import light.head.model.OperationLog;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomNumberEditor;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.multipart.support.ByteArrayMultipartFileEditor;


public class BaseController {
	
	private OperationLogDAO operDAO;


	@InitBinder
	protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
		binder.setAutoGrowCollectionLimit(5000);
		
		binder.registerCustomEditor(Date.class, new CustomDatetimeEditor());
		binder.registerCustomEditor(Byte.class, null, new CustomNumberEditor(Byte.class, null, true));
		binder.registerCustomEditor(Integer.class, null, new CustomNumberEditor(Integer.class, null, true));
		binder.registerCustomEditor(Long.class, null, new CustomNumberEditor(Long.class, null, true));
		binder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor());
	}
	
	// 创建分页对象
	protected PageBean createPageBean(HttpServletRequest request) {
		String pageSize = "";
		pageSize = request.getParameter(Sys.PAGE_SIZE);
		if (!isNotNull(pageSize)) {
			Object obj = request.getSession().getAttribute(Sys.PAGE_SIZE);
			if (null != obj && !Sys.NONE.equals(obj)) {
				pageSize = obj.toString();
			} else {
				pageSize = ConfigParm.getPageSize().toString();
			}
		}
		
		if (StringUtils.isNotEmpty(pageSize))
			return new PageBean(request.getParameter(Sys.PAGE), pageSize);
		else 
			return new PageBean(request.getParameter(Sys.PAGE), ConfigParm.getPageSize());
	}
	
	/**
	 * 直接从response输出内容（通常用作ajax返回）
	 */
	protected boolean print(HttpServletResponse response, String msg) {
		try {
			response.setHeader("Content-Type", "text/html; charset=UTF-8");
			response.getWriter().print(msg);
			return true;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}


	/**
	 * 判断字符串是否不为null和空字符串
	 * @return	为null或空字符串就返回false，否则true
	 */
	protected boolean isNotNull(String str) {
		return null == str || Sys.NONE.equals(str) ? false : true;
	}
	
	/**
	 * 根据参数名称获取查询条件Map
	 */
	protected Map<String, String> createQueryMapByParmName(HttpServletRequest request, final String...names) {
		if (null == names || 0 == names.length)
			return null;
		
		Map<String, String> map = new HashMap<String, String>();
		for (String name : names) {
			map.put(name, request.getParameter(name));
		}
		return map;
	}
	
	/**
	 * 封装查询列表
	 */
	protected List<Object> createParmList(Object...values) {
		if (null == values || values.length == 0)
			return null;
		
		List<Object> parms = new ArrayList<Object>();
		for (Object o : values) {
			parms.add(o);
		}
		return parms;
	}
	
	
	protected Integer cu_code(HttpServletRequest request) {
		Integer cu_code = (Integer)request.getSession().getAttribute(Parm.CU_CODE);
		return cu_code;
	}
	protected Integer cu_roleCode(HttpServletRequest request) {
		Integer cu_roleCode = (Integer)request.getSession().getAttribute(Parm.CU_ROLE_CODE);
		return cu_roleCode;
	}
	protected Integer cu_organCode(HttpServletRequest request) {
		Integer cu_organCode = (Integer)request.getSession().getAttribute(Parm.CU_ORGAN_CODE);
		return cu_organCode;
	}
	protected Integer cu_goodsTypeCode(HttpServletRequest request) {
		Integer cu_goodsTypeCode = (Integer)request.getSession().getAttribute(Parm.CU_GOODSTYPE_CODE);
		return cu_goodsTypeCode;
	}
	protected String cu_organName(HttpServletRequest request) {
		String cu_organName = (String)request.getSession().getAttribute(Parm.CU_ORGAN_NAME);
		return cu_organName;
	}
	protected String cu_realName(HttpServletRequest request) {
		String cu_realName = (String)request.getSession().getAttribute(Parm.CU_REAL_NAME);
		return cu_realName;
	}
	protected Integer cu_rootOrganCode(HttpServletRequest request) {
		Integer cu_rootOrganCode = (Integer)request.getSession().getAttribute(Parm.CU_ROOT_ORGAN_CODE);
		return cu_rootOrganCode;
	}
	protected String cu_email(HttpServletRequest request) {
		String cu_email = (String)request.getSession().getAttribute(Parm.CU_EMAIL);
		return cu_email;
	}
	protected Integer cu_emailCode(HttpServletRequest request) {
		Integer cu_emailCode = (Integer)request.getSession().getAttribute(Parm.CU_EMAILCODE);
		return cu_emailCode;
	}
	
	
	protected String code(HttpServletRequest request) {
		String cu_code = (String)request.getSession().getAttribute(Parm.CU_CODE);
		return cu_code;
	}
	
	protected Integer cu_number(HttpServletRequest request) {
		Integer cu_number = (Integer)request.getSession().getAttribute(Parm.CU_NUMBER);
		return cu_number;
	}
	
	protected OperationLog createLog(String cu, Integer oc, Byte module, String  operation, String kw) {
		OperationLog ol = new OperationLog();
		ol.setCu(cu);
		ol.setCt(new Date());
		ol.setOc(oc);
		ol.setModule(module);
		ol.setOperation(operation);
		ol.setKw(kw);
		
		return ol;
	}
	
	protected void recordLog(String cu, Integer oc, Byte module, String  operation, String kw) {
		OperationLog ol = new OperationLog();
		ol.setCu(cu);
		ol.setCt(new Date());
		ol.setOc(oc);
		ol.setModule(module);
		ol.setOperation(operation);
		ol.setKw(kw);
		
		
		operDAO.add(ol);
	}
	
	
	@Autowired
	public void setOperDAO(OperationLogDAO operDAO) {
		this.operDAO = operDAO;
	}
	
}
